import Ember from 'ember';
import Changeset from 'ember-changeset';
import PlanexeValidations from '../../../validations/planexe';
import lookupValidator from 'ember-changeset-validations';

export default Ember.Controller.extend(PlanexeValidations,{
  constants:Constants,//引入字典常量
  global_dataLoader: Ember.inject.service('data-loader'),
  dateService: Ember.inject.service("date-service"),
  detailEdit:false,
  planModel:Ember.computed('planInfo',function(){
    var model = this.get("planInfo");
    if (!model) {
        return null;
    }
    return new Changeset(model, lookupValidator(PlanexeValidations), PlanexeValidations);
  }),
  customerList:Ember.computed('nursingplanList',function(){
    let nursingplanList = this.get('nursingplanList');
    let customerList = new Ember.A();
    if(nursingplanList){
      nursingplanList.forEach(function(plan){
        let customer = Ember.Object.create({
          id:plan.get('customer.id'),
          name:plan.get('customer.name'),
          item:plan.get('customer'),
          plan:plan,
          sortName:pinyinUtil.getFirstLetter(plan.get('customer.name')),
        });
        customerList.pushObject(customer);
      });
    }
    return customerList;
  }),
  staffList:Ember.computed('customer','staffcustomerList',function(){
    let staffcustomerList = this.get('staffcustomerList');
    let curCustomer = this.get('customer');
    let staffList = new Ember.A();
    if(staffcustomerList){
      staffcustomerList.forEach(function(staffcustomer){
        let staff = Ember.Object.create({
          id:staffcustomer.get('id'),
          name:staffcustomer.get('staff.name'),
          item:staffcustomer.get('staff'),
          sortName:pinyinUtil.getFirstLetter(staffcustomer.get('staff.name'))
        });
        if(staffcustomer.get('customer.id')==curCustomer.get('id')){
          staffList.pushObject(staff);
          console.log(staffcustomer.get('staff.name'));
        }
      });
    }
    return staffList;
  }),
  serviceList:Ember.computed('countType','planServiceList','projectServiceList',function(){
    let projectServiceList = this.get('projectServiceList');
    let countType = this.get('countType');
    let planServiceList = this.get('planServiceList');
    let serviceList = new Ember.A();
    if(countType&&countType.get('id')==165){
      if(projectServiceList){
        projectServiceList.forEach(function(project){
          let service = Ember.Object.create({
            id:project.get('id'),
            name:project.get('item.name'),
            item:project.get('item'),
            project:project,
            sortName:pinyinUtil.getFirstLetter(project.get('item.name'))
          });
          console.log(project.get('id'));
          serviceList.pushObject(service);
        });
      }
    }
    if(countType&&countType.get('id')==164){
      if(planServiceList){
        planServiceList.forEach(function(plan){
          let service = Ember.Object.create({
            id:plan.get('id'),
            name:plan.get('item.item.name'),
            item:plan.get('item'),
            plan:plan,
            sortName:pinyinUtil.getFirstLetter(plan.get('item.name'))
          });
          console.log(plan.get('id'),service.get('plan'));
          serviceList.pushObject(service);
        });
      }
    }
    return serviceList;
  }),
  actions:{
    countTypeSelect(countType){
      let _self = this;
      this.set('countType',countType);
      if(this.get('countType.id')==164){
        //获取护理计划中护理项目列表
        _self.store.query('nursingplanitem',{filter:{plan:{customer:{id:_self.get('customer.id')}},weekTab:_self.get('workDay')}}).then(function(planServiceList){
          _self.set('planServiceList',planServiceList);
        });
      }
      if(this.get('countType.id')==165){
        //获取方案的护理项目列表
        _self.store.query('nursingprojectitem',{filter:{project:{customer:{id:_self.get('customer.id')}},item:{countType:{id:this.get('countType.id')}}}}).then(function(projectServiceList){
          _self.set('projectServiceList',projectServiceList);
        });
      }
    },
    dpShowAction(e){},
    //选择开始时间
    changePlanStartDateAction(date){
      let _self = this;
      //当前选择时间
      var stamp = this.get("dateService").timeStringToTimestamp(date);
      let workDay = this.get("dateService").timestampToTime(stamp).getDay();
      this.set("planModel.exeStartTime", stamp);
      this.set('workDay',workDay);
    },
    selectCustomer(customer){
      let _self=this;
      this.set('customer',customer);
      this.set('plan',customer.get('plan'));
      this.set('planModel.customer',customer.get('item'));
    },
    selectStaff(staff){
      this.set('staff',staff);
      this.set('planModel.exeStaff',staff.get('item'));
    },
    selectService(service){
      this.set('service',service);
      this.set('planModel.service',service.get('item'));
    },
    invalid() {
        //alert("error");
    },
    //保存
    detailSaveClick: function() {
      var planModel=this.get('planModel');
      var _self=this;
      planModel.validate().then(function(){

        if(planModel.get('errors.length')===0){
          App.lookup('controller:business.mainpage').openPopTip("正在保存");
          let planExe = _self.get('planInfo');
          planExe.set('exeStaff',planModel.get('exeStaff'));
          planExe.set('plan',_self.get('plan'));
          planExe.set('exeStartTime',planModel.get('exeStartTime'));

          if(_self.get('countType.id')==164){
            planExe.set('itemPlan',_self.get('service.plan'));
            let json = {};
            json.serviceDesc = _self.get('planInfo.remark');
            let remark = JSON.stringify(json);
            planExe.set('remark',remark);
          }
          if(_self.get('countType.id')==165){
            planExe.set('itemProject',_self.get('service.project'));
            let arr = [];
            let json = {};
            json.content = _self.get('planInfo.remark');
            json.applyUserId = planModel.get('exeStaff.id');
            arr = arr.concat(json);
            let remark = JSON.stringify(arr);
            planExe.set('remark',remark);

          }
          planExe.save().then(function(){
            App.lookup('controller:business.mainpage').showPopTip("保存成功");
              var mainpageController = App.lookup('controller:business.mainpage');
              mainpageController.switchMainPage('serviceapplycheck');
        });
      }else{
        planModel.set("validFlag",Math.random());
      }
      });
    },
    //取消
    detailCancel:function(){
        let mainpageController = App.lookup('controller:business.mainpage');
        mainpageController.switchMainPage('serviceapplycheck');
    },
    //删除按钮
    delById : function() {
      var _self = this;
      App.lookup('controller:business.mainpage').showConfirm("是否确定删除此记录",function(){
        _self.send('cancelPassSubmit',_self.get('planInfo'));
      });
    },
    //弹窗取消
    invitation(){
      this.set('showpopInvitePassModal',false);
    },
    //弹窗确定，删除记录
    cancelPassSubmit(plan){
      App.lookup('controller:business.mainpage').openPopTip("正在删除");
			this.set("showpopInvitePassModal",false);
      plan.set("delStatus", 1);
      plan.save().then(function() {
          App.lookup('controller:business.mainpage').showPopTip("删除成功");
          var mainpageController = App.lookup('controller:business.mainpage');
          mainpageController.switchMainPage('serviceapplycheck');

      });
		},
  }
});
